<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");

/**
 * Description of MerchantCom
 *
 * @author Administrator
 */
class MerchantCom {

    //put your code here
    function __construct() {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }

    function getNumber($str) {
        try {
            if (empty($str)) {
                $sql = "select count(1) from merchant";
            } else {
                $sql = "select count(1) from merchant where mobile like '%{$str}%' or name like '%{$str}%'";
            }
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            return false;
        }
        return 0;
    }

    function getAllMerchant($str, $minlimit, $count) {
        try {
            if (empty($str)) {
                $sql = "select * from merchant order by id desc limit {$minlimit},{$count}";
            } else {
                $sql = "select * from merchant where mobile like '%{$str}%' or name like '%{$str}%' order by id desc limit {$minlimit},{$count}";
            }
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return null;
    }

    function addMerchant($code, $name, $mobile, $address) {
        try {
            $sql = "insert into merchant(code,name,mobile,address) values('{$code}','{$name}','{$mobile}','{$address}')";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    
    function getMerchant($id){
        try{
            $sql = "select * from merchant where id={$id}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret [0][0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    
    function saveMerchant($id,$name,$mobile,$address){
        try{
            $sql = "update merchant set name='{$name}',mobile='{$mobile}',address='{$address}' where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    
    function deleteMerchant($id){
        try{
            $sql = "delete from merchant where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    
}
